package org.jground.rx.ui.zk;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.IRXObjectSynopsis;
import org.jground.rx.RXDataState;
import org.jground.rx.ui.IUIStyles;
import org.jground.util.ArrayUtils;
import org.jground.util.StringUtils;
import org.zkoss.image.AImage;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Label;

public class UIStyles implements IUIStyles<Component> {

    private static Log logger = LogFactory.getLog(UIStyles.class);
    private static UIStyles instance;

    public static UIStyles getInstance() {
        return instance == null ? instance = new UIStyles() : instance;
    }

    public static final String TITLE1 = "TITLE1";
    public static final String BAR = "BAR";
    public static final String BUTTONGROUP = "BUTTONGROUP";
    public static final String GROUP = "BUTTONGROUP";
    public static final String UI_LABEL = "UI_LABEL";
    public static final String UI_MANDATORY_TEXT = "UI_MANDATORY_TEXT";
    public static final String UI_CONTROL_INLINE = "UI_CONTROL_INLINE";

    // public static final String REPORT_HEADER = "REPORT_HEADER";

    private HashMap<String, String> imageUris = new HashMap<String, String>();
    private HashMap<String, AImage> images = new HashMap<String, AImage>();
    private File imageRealDir;
    private String imageDirUri;

    public String getTopLevelSubTitleStyle(String moid) {
        return "ZK_TL_ST";
    }

    public String getTopLevelTitleStyle(String moid) {
        return "ZK_TL_T";
    }

    public Component getTypeIcon(String moid) {
        String[] s = StringUtils.split(moid, '.');
        String name = ArrayUtils.getLast(s);
        String iconLetters = null;
        if (name.length() > 2) {
            iconLetters = name.substring(0, 3);
        } else {
            iconLetters = name;
        }
        return new Label(iconLetters);
    }

    public String getInfoBarStyle() {
        return "ZK_IB";
    }

    public String getFooterStyle() {
        return "";
    }

    public String getModalWindowStyle() {
        return "MODAL_WINDOW";
    }

    public void initIcons(File imageRealDir, String imageDirUri) {
        this.imageRealDir = imageRealDir;
        this.imageDirUri = imageDirUri;
        logger.debug("imageDirUri : " + this.imageDirUri);
        for (File image : imageRealDir.listFiles()) {
            if (image.isFile()) {
                String key = FilenameUtils.getBaseName(image.getName());
                // imageUris.put(key,
                // FilenameUtils.separatorsToUnix(FilenameUtils.concat(imageDirUri,
                // image.getName())));
                imageUris.put(key, image.getName());
            }
        }
    }

    public AImage getImage(String moid, String size, RXDataState state) {
        String imageKey = moid + "-" + size + "-" + state.toAbbr();
        AImage aimage = getAImage(imageKey);
        if (aimage == null) {
            imageKey = moid + "-" + size;
            aimage = getAImage(imageKey);
            if (aimage == null) {
                imageKey = moid + "-" + size;
                aimage = getAImage("moid-" + size);
                if (aimage == null) {
                    aimage = getAImage(ImageKey.notfound16);
                }
            }
        }
        return aimage;
    }

    public AImage getImage(IRXObjectSynopsis mod, String size) {
        return getImage(mod.getMoid(), size, mod.getDataState());
    }

    public AImage getAImage(String imageKey) {
        AImage aimage = null;
        try {
            aimage = images.get(imageKey);
            if (aimage == null) {
                String imageName = imageKey + ".png";
                InputStream in = this.getClass().getClassLoader().getResourceAsStream(imageName);
                if (in != null) {
                    aimage = new AImage(imageName, in);
                } else {
                    String file = imageUris.get(imageKey);
                    if (StringUtils.isNotBlank(file)) {
                        File imageFile = new File(imageRealDir, file);
                        if (imageFile.exists()) {
                            aimage = new AImage(imageFile);
                        }
                    }
                }
                this.images.put(imageKey, aimage);
            }
        } catch (Exception e) {
            logger.error(e, e);
        }
        return aimage;
    }

    public String getImageUri(String moid, String size) {
        String key = moid + "-" + size;
        String icon = imageUris.get(key);
        if (icon == null) {
            icon = imageUris.get("moid-" + size);
        }
        return returnNotNull(icon);
    }

    public String getImageUri(String moid, String size, RXDataState state) {
        String key = moid + "-" + size + "-" + state.toAbbr();
        String icon = imageUris.get(key);
        if (icon == null) {
            key = moid + "-" + size;
            icon = imageUris.get(key);
            if (icon == null) {
                icon = imageUris.get("moid-" + size);
            }
        }
        icon = returnNotNull(icon);

        return this.imageDirUri + "/" + icon;
    }

    public String getImageUri(IRXObject mod, String size) {
        return getImageUri(mod.getMoid(), size, mod.getDataState());
    }

    private String returnNotNull(String icon) {
        return icon == null ? "notfound.png" : icon;
    }

    public int getMaxWidth() {
        return 1200;
    }

    //
    // Component Factory
    //
    public Label getAttributeLabel(String label) {
        return getAttributeLabel(label, false);
    }

    public Label getAttributeLabel(String label, boolean mandatory) {
        Label attributeLabel = new Label(label);
        // attributeLabel.setStyle("font-weight:bold;white-space:nowrap");
        // if (mandatory) {
        // attributeLabel.setStyle("text-decoration:underline");
        // }
        attributeLabel.setStyle("text-decoration:underline;white-space:nowrap");
        return attributeLabel;
    }

}
